/**
 * 许可证状态获取接口测试用例
 *
 * Created by snowingsea on 2020/02/25.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../common');
const Administrator = require('./../../models/administrator');
const uri = require('./uri');

const { statusKind } = require('./../../../phoenix/models/license');
const license = require('./../../../phoenix/models/license');

module.exports = describe('/GET license/status.json', function () {
  const administrator = new Administrator();
  const url = '/license/status.json';
  const newUserIds = [];
  let tester;

  before(function (done) {
    administrator.createRandomTester()
      .then((res) => {
        tester = res;
        newUserIds.push(tester.userId);
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');

  // 正常请求
  [
    statusKind.normal,
    statusKind.illegal,
    statusKind.notMatch,
    statusKind.exceedTime,
    statusKind.exceedTester,
    statusKind.exceedTime + statusKind.exceedTester,
  ].forEach((status, index) => {
    it(`it should ${index === 0 ? 'GET' : 'get'} license status of ${status}`, function (done) {
      administrator.replaceLicense(status)
        .then(() => tester.login())
        .then(() => {
          chai.request(uri)
            .get(url)
            .set('Authorization', `Bearer ${tester.token.accessToken}`)
            .end((err, res) => {
              res.should.have.status(200);
              res.body.should.be.a('object');
              res.body.should.have.property('licenseStatus');
              res.body.licenseStatus.should.be.a('number');
              res.body.licenseStatus.should.be.eql(status);
              done();
            });
        })
        .catch(done);
    });
  });

  // 不可获取不存在的许可证状态
  it('it should fail to get license status when the license is not exists', function (done) {
    administrator.replaceLicense(statusKind.normal)
      .then(() => administrator.removeLicense())
      .then(() => tester.login())
      .then(() => {
        chai.request(uri)
          .get(url)
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .end((err, res) => {
            administrator.replaceLicense(license.statusKind.normal)
              .then(() => {
                res.should.have.status(404);
                common.should.be.errorMessage(res.body);
                done();
              })
              .catch(done);
          });
      })
      .catch(done);
  });
});
